package com.lazy.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lazy.dto.CategoryDTO;
import com.lazy.dto.CategoryPageQueryDTO;
import com.lazy.entity.repair.Category;
import com.lazy.entity.repair.RepairCategoriesSubcategory;
import com.lazy.mapper.CategoryMapper;
import com.lazy.result.PageResult;
import com.lazy.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> result = page.getResult();
        return new  PageResult(total,result);
    }

    @Override
    public List<RepairCategoriesSubcategory> selectSubcategory() {
        return categoryMapper.selectSubcategory();
    }

    @Override
    public List<Category> selectCategory() {
        return categoryMapper.selectCategory();
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        RepairCategoriesSubcategory repairCategoriesSubcategory = RepairCategoriesSubcategory.builder()
                .categoryId(categoryDTO.getCid())
                .name(categoryDTO.getName())
                .updateTime(LocalDateTime.now())
                .createTime(LocalDateTime.now())
                .build();
        categoryMapper.add(repairCategoriesSubcategory);
    }

    @Override
    public void delete(List<Integer> id) {
        categoryMapper.delete(id);
    }

    @Override
    public Category selectById(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    @Transactional
    public void update(CategoryDTO categoryDTO) {
        RepairCategoriesSubcategory repairCategoriesSubcategory = RepairCategoriesSubcategory.builder()
                .id(categoryDTO.getId())
                .categoryId(categoryDTO.getCid())
                .name(categoryDTO.getName())
                .updateTime(LocalDateTime.now())
                .build();

        log.info("修改分类:{}",repairCategoriesSubcategory);
        Integer update = categoryMapper.update(repairCategoriesSubcategory);
        System.out.println("成功更新了" + update + "数据");
    }
}
